 package cn.edu.ujn.finalwork.c;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.ujn.finalwork.dao.Base;
import cn.edu.ujn.finalwork.dao.User;
import cn.edu.ujn.finalwork.service.IBaseService;

@Controller
 public class BaseController {

    @Autowired
    private IBaseService baseService;
    private String tName;
    @GetMapping("/returnC")
    public String returnC(HttpSession session) {
        List<String> tn=baseService.type();
        session.setAttribute("tn", tn);
        session.setAttribute("all",baseService.selectAll());
        return "base";
    }
    @GetMapping("/shu")
    public String shu(HttpSession session,Model model)
    {
        User i = (User)session.getAttribute("userr");
        if(i==null||i.getUserState().equals("0"))
{
            model.addAttribute("fail","请登录账号！");
            return "failure";
        }else if(!(i.getUserAuthority().equals("1"))){
            model.addAttribute("fail","您没有权限！");
            return "failure";
        }else {
        tName="全部数据";
        List<String> tn=baseService.type();
        session.setAttribute("all",baseService.selectAll());
        session.setAttribute("tn", tn);
        return "base";}
    }
    @PostMapping("/shuTwo")
    public String shuTwo(HttpSession session,@RequestParam("tName") String type) {
        tName=type;
        if(type.equals("全部数据")) {
            session.setAttribute("all",baseService.selectAll());
        }else {
            session.setAttribute("all",baseService.selectByTName(type));
        }
        return "base";
    }
    @GetMapping("/shuThree")
    public String shuThree(HttpSession session) {
        List<String> list=new ArrayList<String>();
        list.add("按id查找");
        list.add("按类型名称和类型id查找");
        Base base=null;
        session.setAttribute("base", base);
        session.setAttribute("list", list);
        return "baseTwo";
    }
    @PostMapping("/shuFour")
    public String shuFour(HttpSession session,@RequestParam("tTName") String type)
    {
        if(type.equals("按id查找")) {
            return "baseThree";
        }else {
        return "baseFour";}
    }
    @PostMapping("/shuFive")
    public String shuFive(HttpSession session,@RequestParam("id") String id,Model model){
        Base key = baseService.selectByPrimaryKey(id);
        if(key==null) {
            model.addAttribute("fail","该数据不存在！");
            return "failure";
        }else {
           session.setAttribute("select", baseService.selectByPrimaryKey(id));
           return "baseFive"; 
        }
        
    }
    @PostMapping("/shuSix")
    public String shuFive(Model model,HttpSession session,@RequestParam("id") String id,@RequestParam("type") String type){
        Base base=new Base();
        base.setDictTypeCode(id);
        base.setDictTypeName(type);
        Base name = baseService.selectByType_CodeAndType_Name(base);
        if(name==null) {
            model.addAttribute("fail", "该数据不存在！");
            return "failure";
        }else {
        session.setAttribute("select", baseService.selectByType_CodeAndType_Name(base));
        return "baseFive";}
    }
    @GetMapping("/sh")
    public String sh() {
        return "baseG";
    }
    @PostMapping("/shTwo")
    public String shTwo(@RequestParam("op") List<String> op,HttpSession session) {
        for(String s:op) {
            baseService.deleteByPrimaryKey(s);
        }
        if(tName.equals("全部数据")) {
        List<Base> list = baseService.selectAll();
        session.setAttribute("all", list);
        }else {
        List<Base> list = baseService.selectByTName(tName);
        
        session.setAttribute("all", list);
        }
        List<String> tn=baseService.type();
        session.setAttribute("tn", tn);
        return "base";
    }
    @GetMapping("/tian")
    public String tian() {
        return "addBase";
    }
    @GetMapping("/xiugaiB")
    public String xiugai() {
        return "reviseB";
    }
    @PostMapping("/xiugaiB2")
    public String xiugaiB2(HttpSession session,@RequestParam("IName") String IName,@RequestParam("TCode") String TCode,@RequestParam("TName") String TName,@RequestParam("ICode") String ICode,@RequestParam("sort") String sort,@RequestParam("able") String able,@RequestParam("memo") String memo) {
       Base a = (Base)session.getAttribute("b");
       int i=Integer.parseInt(sort);
       Base base=new Base();
       base.setDictItemName(IName);
       base.setDictTypeCode(TCode);
       base.setDictTypeName(TName);
       base.setDictSort(i);
       base.setDictEnable(able);
       base.setDictMemo(memo);
       base.setDictItemCode(ICode);
       base.setDictId(a.getDictId());
       int selective = baseService.updateByPrimaryKeySelective(base);
       Base b=baseService.selectByPrimaryKey(a.getDictId());
       List<String> tn=baseService.type();
       session.setAttribute("tn", tn);
       session.setAttribute("select", b);
       return "baseFive";
    }
    @PostMapping("/tian2")
    public String tian2(HttpSession session,@RequestParam("Id") String Id,@RequestParam("IName") String IName,@RequestParam("TCode") String TCode,@RequestParam("TName") String TName,@RequestParam("ICode") String ICode,@RequestParam("sort") String sort,@RequestParam("able") String able,@RequestParam("memo") String memo) {
       int i=Integer.parseInt(sort);
       Base base=new Base();
       base.setDictId(Id);
       base.setDictItemName(IName);
       base.setDictTypeCode(TCode);
       base.setDictTypeName(TName);
       base.setDictSort(i);
       base.setDictEnable(able);
       base.setDictMemo(memo);
       base.setDictItemCode(ICode);
       baseService.insertSelective(base);
       List<String> tn=baseService.type();
       session.setAttribute("tn", tn);
       session.setAttribute("select", base);
       return "baseFive";
    }
    @GetMapping("/shanchuB")
    public String shanchuB(HttpSession session) {
        List<String> tn=baseService.type();
        session.setAttribute("tn", tn);
        Base a = (Base)session.getAttribute("b");
        baseService.deleteByPrimaryKey(a.getDictId());
        return "base";
    }
}
